function lazyman(name){
    this.task = []
    this.task.push(()=>{
        return new Promise(resolve =>{
            console.log("name: "+name)
            resolve()
        })
    })
    // 重点
    let run = ()=>{
        let queue = Promise.resolve()
        for(const func of this.task){
            // then 返回Promise
            queue = queue.then(()=>func())
        }
    }
    setTimeout(()=>{run()},0)
    this.eat = food => {
        this.task.push(()=>{
            return new Promise(resolve=>{
                console.log("eat: "+food)
                resolve()
            })
        })
        return this
    }
    this.sleep = time=>{
        this.task.push(()=>{
            return new Promise(resolve => {
                setTimeout(()=>{
                    console.log("wake up after "+ time)
                    resolve()
                },time)
            })
        })
        return this
    }
    this.sleepFirst = time => {
        this.task.unshift(()=>{
            return new Promise(resolve=>{
                setTimeout(()=>{
                    console.log("sleepFirst up after "+time)
                    resolve()
                },time)
            })
        })
        return this
    }
}
new lazyman("lihua").sleep(1000).eat("yeye").sleepFirst(2000)